const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const isProd = process.env.NODE_ENV === 'production';

function parseEnv() {
  const localEnv = path.resolve('.env');
  const prodEnv = path.resolve('.env.prod');

  if (!fs.existsSync(localEnv) && !fs.existsSync(prodEnv)) {
    throw new Error('缺少环境配置文件');
  }

  const filePath = isProd && fs.existsSync(prodEnv) ? prodEnv : localEnv;

  const config = dotenv.parse(fs.readFileSync(filePath));

  const res = {};
  Object.keys(config).forEach(key => {
    res[`process.env.${key}`] = JSON.stringify(config[key]);
  });

  console.log('config', res)

  return res;
}

module.exports = parseEnv();
